package com.ncmmall.application.business.marketing;

import com.ncmmall.domain.business.goods.Goods;
import com.ncmmall.domain.business.goods.QGoods;
import com.ncmmall.domain.business.goods.channel.GoodsChannel;
import com.ncmmall.domain.business.goods.channel.QGoodsChannel;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.primitives.Longs;
import com.ncmmall.domain.business.marketing.gift.GiftActivity;
import com.ncmmall.domain.business.marketing.gift.GiftActivityRepository;
import com.ncmmall.domain.business.marketing.gift.GiftChannel;
import com.ncmmall.domain.business.marketing.gift.GiftChannelRepository;
import com.ncmmall.domain.business.marketing.gift.GiftGoods;
import com.ncmmall.domain.business.marketing.gift.GiftRepository;
import com.ncmmall.domain.business.marketing.gift.GiftScope;
import com.ncmmall.domain.business.marketing.gift.GiftScopeRepository;
import com.ncmmall.domain.business.marketing.gift.QGiftActivity;
import com.ncmmall.domain.business.marketing.gift.QGiftChannel;
import com.ncmmall.domain.business.marketing.gift.QGiftGoods;
import com.ncmmall.domain.business.marketing.gift.QGiftScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nullable;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author linxs
 * @Description: 赠品活动业务
 * @Date: 2020-01-10 17:41
 */
@Service
@Transactional
public class GiftApplication {

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private GiftRepository giftRepository;

    @Autowired
    private GiftActivityRepository giftActivityRepository;

    @Autowired
    private GiftScopeRepository giftScopeRepository;

    @Autowired
    private GiftChannelRepository giftChannelRepository;

    /**
     * 添加赠品活动
     *
     * @param giftActivity
     */
    public void create(GiftActivity giftActivity) {

        List<String> giftGoodsIds = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(giftActivity.getGiftGoodsesString());

        List<Goods> gifts = Lists.transform(giftGoodsIds, new Function<String, Goods>() {
            @Nullable
            @Override
            public Goods apply(@Nullable String input) {

                return queryChannelService.findOne(QGoods.goods.id.eq(Longs.tryParse(input)), Goods.class);
            }
        });

        List<String> scopeGoodsIds = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(giftActivity.getScopeGoodsesString());
        List<Goods> scopeGoods = Lists.transform(scopeGoodsIds, new Function<String, Goods>() {
            @Nullable
            @Override
            public Goods apply(@Nullable String input) {
                return queryChannelService.findOne(QGoods.goods.id.eq(Longs.tryParse(input)), Goods.class);
            }
        });

        List<String> scopeChannelIds = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(giftActivity.getChannelsString());
        List<GoodsChannel> channels = Lists.transform(scopeChannelIds, new Function<String, GoodsChannel>() {
            @Nullable
            @Override
            public GoodsChannel apply(@Nullable String input) {
                return queryChannelService.findOne(QGoodsChannel.goodsChannel.id.eq(Longs.tryParse(input)), GoodsChannel.class);
            }
        });


        StringBuilder names = new StringBuilder();
        StringBuilder images = new StringBuilder();

        for (Goods gift : gifts) {
            names.append(gift.getName() + ";");
            images.append(gift.getDefaultImage() + ";");
        }

        giftActivity.setGiftNames(names.toString());
        giftActivity.setGiftImages(images.toString());
        giftActivity.setIsValidity(1);
        giftActivity.setStatus(2);
        giftActivity.setCreateTime(new Timestamp(System.currentTimeMillis()));
        giftActivityRepository.save(giftActivity);


        for (Goods goods : gifts) {
            GiftGoods gift = new GiftGoods();
            gift.setGoods(goods);
            gift.setGift(giftActivity);
            gift.setMerchant(giftActivity.getMerchant());
            giftRepository.save(gift);
        }

        if (giftActivity.getScopeGoodsType() == 2) {
            for (Goods goods : scopeGoods) {
                GiftScope scope = new GiftScope();
                scope.setGift(giftActivity);
                scope.setGoods(goods);
                scope.setMerchant(giftActivity.getMerchant());
                giftScopeRepository.save(scope);
            }
        }

        if (giftActivity.getScopeGoodsType() == 3) {
            for (GoodsChannel channel : channels) {
                GiftChannel giftChannel = new GiftChannel();
                giftChannel.setChannel(channel);
                giftChannel.setMerchant(giftActivity.getMerchant());
                giftChannel.setGift(giftActivity);
                giftChannelRepository.save(giftChannel);
            }
        }

    }

    /**
     * 提交审核
     *
     * @param giftActivity
     */
    public void applyProcess(GiftActivity giftActivity) {
        giftActivity.setStatus(3);
        giftActivityRepository.save(giftActivity);
    }

    /**
     * 上架
     *
     * @param giftActivity
     */
    public void putaway(GiftActivity giftActivity) {
        giftActivity.setStatus(1);
        giftActivityRepository.save(giftActivity);
    }

    /**
     * 下架
     *
     * @param giftActivity
     */
    public void soldout(GiftActivity giftActivity) {
        giftActivity.setStatus(2);
        giftActivityRepository.save(giftActivity);
    }

    /**
     * 逻辑删除
     *
     * @param giftActivity
     */
    public void remove(GiftActivity giftActivity) {
        giftActivity.setStatus(6);
        giftActivityRepository.save(giftActivity);
    }

    /**
     * 审核赠品活动
     *
     * @param params
     */
    public void process(GiftActivity params) {
        GiftActivity giftActivity = queryChannelService.findOne(QGiftActivity.giftActivity.id.eq(params.getId()), GiftActivity.class);
        giftActivity.setStatus(params.getStatus());
        giftActivity.setNote(params.getNote());
        giftActivityRepository.save(giftActivity);
    }

    /**
     *
     * @param params
     */
    public void modify(GiftActivity params) {

        GiftActivity giftActivity = queryChannelService.findOne(QGiftActivity.giftActivity.id.eq(params.getId()), GiftActivity.class);
        giftActivity.setName(params.getName());
        giftActivity.setBeginDateOfValidity(params.getBeginDateOfValidity());
        giftActivity.setEndDateOfValidity(params.getEndDateOfValidity());
        giftActivity.setStandardsAmount(params.getStandardsAmount());

        List<String> giftGoodsIds = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(params.getGiftGoodsesString());

        List<Goods> gifts = Lists.transform(giftGoodsIds, new Function<String, Goods>() {
            @Nullable
            @Override
            public Goods apply(@Nullable String input) {

                return queryChannelService.findOne(QGoods.goods.id.eq(Longs.tryParse(input)), Goods.class);
            }
        });

        List<String> scopeGoodsIds = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(params.getScopeGoodsesString());
        List<Goods> scopeGoods = Lists.transform(scopeGoodsIds, new Function<String, Goods>() {
            @Nullable
            @Override
            public Goods apply(@Nullable String input) {
                return queryChannelService.findOne(QGoods.goods.id.eq(Longs.tryParse(input)), Goods.class);
            }
        });

        List<String> scopeChannelIds = Splitter.on("-").omitEmptyStrings().trimResults().splitToList(params.getChannelsString());
        List<GoodsChannel> channels = Lists.transform(scopeChannelIds, new Function<String, GoodsChannel>() {
            @Nullable
            @Override
            public GoodsChannel apply(@Nullable String input) {
                return queryChannelService.findOne(QGoodsChannel.goodsChannel.id.eq(Longs.tryParse(input)), GoodsChannel.class);
            }
        });


        StringBuilder names = new StringBuilder();
        StringBuilder images = new StringBuilder();

        for (Goods gift : gifts) {
            names.append(gift.getName() + ";");
            images.append(gift.getDefaultImage() + ";");
        }

        giftActivity.setGiftNames(names.toString());
        giftActivity.setGiftImages(images.toString());
        giftActivity.setScopeGoodsType(params.getScopeGoodsType());
        giftActivityRepository.save(giftActivity);

        giftRepository.delete(queryChannelService.findAll(QGiftGoods.giftGoods.gift.eq(giftActivity), GiftGoods.class));
        giftScopeRepository.delete(queryChannelService.findAll(QGiftScope.giftScope.gift.eq(giftActivity), GiftScope.class));
        giftChannelRepository.delete(queryChannelService.findAll(QGiftChannel.giftChannel.gift.eq(giftActivity), GiftChannel.class));

        for (Goods goods : gifts) {
            GiftGoods gift = new GiftGoods();
            gift.setGoods(goods);
            gift.setGift(giftActivity);
            gift.setMerchant(giftActivity.getMerchant());
            giftRepository.save(gift);
        }

        if (params.getScopeGoodsType() == 2) {
            for (Goods goods : scopeGoods) {
                GiftScope scope = new GiftScope();
                scope.setGift(giftActivity);
                scope.setGoods(goods);
                scope.setMerchant(giftActivity.getMerchant());
                giftScopeRepository.save(scope);
            }
        }

        if (params.getScopeGoodsType() == 3) {
            for (GoodsChannel channel : channels) {
                GiftChannel giftChannel = new GiftChannel();
                giftChannel.setChannel(channel);
                giftChannel.setMerchant(giftActivity.getMerchant());
                giftChannel.setGift(giftActivity);
                giftChannelRepository.save(giftChannel);
            }
        }

    }
}
